from appium import webdriver
from appium.options.common import AppiumOptions


class Driver:
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = object.__new__(cls)
        return cls._instance

    def __init__(self):
        if not hasattr(self, "driver"):
            self.driver = None


    def create_driver(self):

        options = AppiumOptions()
        options.load_capabilities({
            "platformName": "Android",
            "appium:automationName": "UiAutomator2",
            "appium:ensureWebviewsHavePages": True,
            "appium:nativeWebScreenshot": True,
            "appium:newCommandTimeout": 3600,
            "appium:noReset": True,
            "appium:connectHardwareKeyboard": True,
            "appium:appPackage": "com.xiaomi.shop",
            "appium:appActivity": ".activity.MainTabActivity ",
            "appium:deviceName": "V2302A",
            "appium:udid": "127.0.0.1:5555"
        })
        self.driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
        self.driver.implicitly_wait(10)
        self.driver.wait_activity(".activity.MainTabActivity ", 5)

    def get_driver(self):
        return self.driver

_driver = Driver()
_driver.create_driver()
DRIVER = _driver.get_driver()
